<script setup lang="ts">
import { ref, defineProps, defineEmits, PropType } from "vue";
import { debounce } from "@pureadmin/utils";
import FromView, {
  type FormConfigItem,
  useFormData
} from "@/components/FormView";
const props = defineProps({
  queryList: {
    type: Array as PropType<FormConfigItem[]>,
    default: () => []
  },
  data: {
    type: Object,
    default: () => {
      return {};
    }
  }
});
const formData = ref<{ [key: string]: any }>({});
const emits = defineEmits(["search"]);
const { resetFormData, handleFormData } = useFormData(
  props.queryList,
  formData.value
);
function handleSubmit() {
  const data = handleFormData();
  emits("search", data);
}
function handleRest() {
  resetFormData();
  emits("search", formData.value);
}

// 对按钮操作做一个防抖处理
const debouncedSubmit = debounce(handleSubmit, 50);
const debouncedReset = debounce(handleRest, 50);
</script>

<template>
  <div class="flex">
    <FromView v-model="formData" :formList="props.queryList" />
    <div class="flex gap-3">
      <el-button type="primary" @click="debouncedSubmit">查询</el-button>
      <el-button type="primary" @click="debouncedReset">重置</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped></style>
